package ext.tianma.part;

import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;

import com.ptc.windchill.enterprise.part.commands.PartDocServiceCommand;

import wt.doc.WTDocument;
import wt.fc.ObjectReference;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.type.ClientTypedUtility;
import wt.util.WTException;
import wt.util.WTRuntimeException;
import ext.com.iba.IBAUtil;
import ext.generic.util.Generic;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.TianMaConstants;

public class CheckPartReveal {
	private static final String CF_SOFT_TYPE=".CF";
	private static final String C4_SOFT_TYPE=".C4";
	private static final String PHANTOM_POLARIZER=".PhantomPolarizer"; //虚拟偏光片组件
	private static final String SEMIFINISHEDGOOD  = ".SemiFinishedGood"; //半成品
	private static final String RAWMATERIAL  = ".RawMaterial"; //原材料
	private static final String Polarizer = ".Polarizer"; //偏光片
	private static final String TopPolarizer = ".TopPolarizer"; //偏光片
	private static final String BotPolarizer = ".BotPolarizer"; //偏光片
	private static String filePath = "/ext/tianma/cfg/TianmaPro.properties"; //偏光片
	/**
	 *如果半成品类型是CF则走采购信息审核节点
	 * @param part
	 * @return
	 * @throws WTException
	 */
	public static String opinionPart(WTPart part) throws WTException{
		String ifsemiGoodAudit="";
		String name = IBAUtil.getSoftType(part).getLogicalIdentifier();
		if(name.contains(CF_SOFT_TYPE)){
			ifsemiGoodAudit="Y";
		}
		return ifsemiGoodAudit;
	}
	/**
	 * 如果类型为半成品不走走集团商务节点
	 * @param part
	 * @return
	 * @throws WTException
	 */
	public static String opinionParts(WTPart part) throws WTException{
		String partStandardCost = "";
		String name = IBAUtil.getSoftType(part).getLogicalIdentifier();
		if( ( !name.contains(CF_SOFT_TYPE) ) && ( !name.contains(C4_SOFT_TYPE) ) ){
			partStandardCost="";
		}
		return partStandardCost;
	}
	
	/**
	 * C4和虚拟偏光片不走“审核信息输入”节点（版本进阶）
	 * @param obj
	 * @return
	 * @throws WTException
	 */
	public static String opinionObj(Object obj) throws WTException{		
		String materialsInfo = "";
		WTPart  part = null;
		if( obj == null ){
			return materialsInfo;
		}
		
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null){
			String type = ClientTypedUtility.getTypeIdentifier(part).getTypename() ;
			System.out.println( "Debug   type:"+type );
			if( ( !type.contains( C4_SOFT_TYPE ) ) && ( !type.contains( PHANTOM_POLARIZER ) ) ){
				materialsInfo="Y";
			}
		}
		return materialsInfo;
	}
	
	/**
	 * 虚拟偏光片不走“审核信息输入”节点 （二供  和 原材料）
	 * @param obj
	 * @return
	 * @throws WTException
	 */
	public static String opinionObjS(Object obj) throws WTException{		
		String materialsInfo = "";
		WTPart  part = null;
		if( obj == null ){
			return materialsInfo;
		}
		
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null){
			String type = ClientTypedUtility.getTypeIdentifier(part).getTypename() ;
			String softType = IBAUtil.getSoftType(part).getLogicalIdentifier();
			
			if( ( !type.contains( PHANTOM_POLARIZER ) ) && (!softType.contains(SEMIFINISHEDGOOD) ) && (!type.contains(C4_SOFT_TYPE ) ) ){
				materialsInfo="Y";
			}
		}
		return materialsInfo;
	}
	
	/**
	 * 只有半成品走“集团商务填写价格”
	 * @param obj
	 * @return
	 */
	public static String docType( Object obj ){
		String partStandardCost = "";
		WTPart  part = null;
		if( obj == null ){
			return partStandardCost;
		}
		

		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null){
			 
			try {
				String softType = IBAUtil.getSoftType(part).getLogicalIdentifier();
				
				if( softType != null ){
					
					if( ( softType.contains( SEMIFINISHEDGOOD ) ) ){
						partStandardCost="";
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			}
			
		}
		
		return partStandardCost;
	}
	
	/**
	 * 判断只有原材料以及偏光片走“法规审核节点”
	 * @param obj
	 * @return
	 */
	public static String setLegislationAudit( Object obj ){
		String legislationAudit = "";
		WTPart  part = null;                  
		if( obj == null ){
			return legislationAudit;
		}
		

		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null){
			 
			try {
				String type = IBAUtil.getSoftType(part).getLogicalIdentifier();
				
				if( type != null ){
					
					if( ( type.contains( RAWMATERIAL ) ) || ( type.contains(Polarizer)) || ( type.contains(TopPolarizer)) || ( type.contains(BotPolarizer)) ){
						legislationAudit="Y";
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			}
			
		}
		return legislationAudit;
	}
	
	/**
	 * 法规审核的节点，判定料号生效地为深圳，是否需要上传环保资料
	 * @param obj
	 * @param flag
	 * @return
	 */
	public static boolean setEnvironmentalDocAudit(Object obj){
		boolean flag = false;
		WTPart  part = null;                  
		if( obj == null ){
			return flag;
		}

		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		if(checkEffectiveSite(part)){
			String partEName = IbaAttributeUtil.getAttributeValue(part, "partCName");
			String isDrawing = IbaAttributeUtil.getAttributeValue(part, "isDrawing");
			partEName = partEName.replaceAll(" ", "");
			if( partEName != null && !"".equals(partEName) ){
				String key = partEName + ".lawAssessor";
				Object value = null;
				try {
					value = Generic.getStrFromProperties(key, filePath);
					if(value != null){
						if("Y".equals(value.toString())){
							flag = true;
						}else if("P".equals(value.toString()) && "否".equals(isDrawing)){
							flag = true;
						}
					}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (WTException e) {
					e.printStackTrace();
				}
				
			}
		}
		
		return flag;
	}
	/**
	 * 判定料号生效地是否包含深圳
	 * @param part
	 * @return boolean
	 */
	private static boolean checkEffectiveSite(WTPart part) {
		List<String> effectiveSite = IbaAttributeUtil.getAttributeValues(part, "partNumberEffectiveSite");
		boolean temp = false;
		for (String site : effectiveSite) {
			if(site.contains(TianMaConstants.EffectiveSite.C01.getSite()) || site.equals(TianMaConstants.EffectiveSite.C02.getSite()) || site.equals(TianMaConstants.EffectiveSite.C04.getSite()) || site.equals(TianMaConstants.EffectiveSite.C08.getSite())){
				temp = true;
				break;
			}
		}
		return temp;
	}
	
	/**
	 * 判断是否走PM审核结点
	 * @param obj
	 * @return
	 */
	public static String setPMAudit(Object obj){
		String PMAudit = "";
		WTPart part = null; 
		if( obj == null ){
			return PMAudit;
		}
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null && TMTypeUtil.isRawMaterial(part)){
			try {
				String partEName = (String) IBAUtil.getIBAValue(part, "partEName");
				if( partEName != null && !"".equals(partEName) ){
					String key = partEName + ".PMPI_ASSESSOR";
					Object value = Generic.getStrFromProperties(key, filePath);
					if(value != null && "Y".equals(value.toString())){
						PMAudit="Y";
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return PMAudit;
	}
	
	/**
	 * 判断是否加签审核
	 * @param obj
	 * @return
	 */
	public static String setcountersignAudit(Object obj){
		String countersignAudit = "";
		WTPart part = null; 
		if( obj == null ){
			return countersignAudit;
		}
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		
		if(part != null && TMTypeUtil.isRawMaterial(part)){
			try {
				String partCName = (String)IBAUtil.getIBAValue(part, "partCName");
				Vector partEffectiveSite = IBAUtil.getIBAValue2(part,"partNumberEffectiveSite");
				System.out.println("setcountersignAudit中文名称："+partCName);
				System.out.println("setcountersignAudit方法料号生效地："+partEffectiveSite);
				if( partCName != null && !"".equals(partCName)){
					if(partEffectiveSite != null && !"".equals(partEffectiveSite) ){
						String effectiveSite = partEffectiveSite.toString();
						if(effectiveSite.contains("C01")||effectiveSite.contains("C02")||effectiveSite.contains("C04")||effectiveSite.contains("C06")||effectiveSite.contains("C08")) {
							String key = partCName + ".HeadAssessor";
							Object value = Generic.getStrFromProperties(key, filePath);
							System.out.println("key值：" + key);
							System.out.println("value值：" + value);
							if(value != null && "Y".equals(value.toString())){
								countersignAudit="Y";
							}
						}
					}
				}
			} catch (WTException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return countersignAudit;
	}
	
	/**
	 * 判断NPM审核节点
	 * 设计转量产确认流程
	 * 上海G4.5  成都G4.5  武汉G4.5物料生效地NPM审核设置为必选，其余物料生效地为可选
	 * @param obj
	 * @return
	 * @throws WTException 
	 */
	public static String setNPMAudit(Object obj){
		String NPMAudit = "";
		WTDocument doc = null; 
		WTArrayList partList = new WTArrayList();
		if( obj == null ){
			return NPMAudit;
		}
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTDocument){
			doc = (WTDocument) obj;
			try {
				partList = PartDocServiceCommand.getAssociatedRefParts(doc);
			} catch (WTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}

		if(partList != null && !"".equals(partList)){
			
			try {
				for(int i = 0;i<partList.size();i++) {
					ObjectReference partref = (ObjectReference)partList.get(i);
					Object partobj = partref.getObject();
					WTPart part = (WTPart)partobj;
							
					Vector partEffectiveSite = IBAUtil.getIBAValue2(part,"partNumberEffectiveSite");
					System.out.println("setNPMAudit方法料号生效地："+partEffectiveSite);
					if(partEffectiveSite != null && !"".equals(partEffectiveSite) ){
						String effectiveSite = partEffectiveSite.toString();
						if(effectiveSite.contains("A10")||effectiveSite.contains("E10")||effectiveSite.contains("F10")) {
							NPMAudit="Y";
						}else {
							NPMAudit="S";
						}
						System.out.println("NPMAudit值:"+NPMAudit);
					}						
				}
			} catch (WTRuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WTException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			
		}
		return NPMAudit;
	}

	//判断版本进阶是否是需要生成新的mark值的物料类
	public static boolean isNewMark(Object obj){
		WTPart part = null;
		if(obj == null){
			return false;
		}
		if(obj instanceof ObjectReference){
			ObjectReference ref = (ObjectReference) obj;
			obj = ref.getObject();
		}
		
		if(obj instanceof WTPart){
			part = (WTPart)obj;
		}
		if(part != null){
			if(TMTypeUtil.isTopPolarizer(part) || TMTypeUtil.isBotPolarizer(part)){
				return true;
			}
		}
		return false;
	}
	
}
